package chap11;
import java.io.*;
import java.util.Scanner;

public class Classex {
    public static void main(String[] args) throws IOException {
        String a;
        Scanner scan = new Scanner(System.in);

        //创建文件。
        File file = new File("C:\\Users\\恺\\IdeaProjects\\GK20172301_JavaProgramming","sort.txt");

        System.out.println("请输入任意个整数(请用空格隔开)：");
        a = scan.nextLine();
        OutputStream outputStream = new FileOutputStream(file);
        char[]   nums = a.toCharArray();

        System.out.println(nums);
        Classex.insertSort(nums);
        System.out.println(nums);
        for (int x = 0; x < nums.length; x++) {
            outputStream.write(nums[x]);
        }
        System.out.println("文件已经创建。");

        InputStream inputStream = new FileInputStream(file);

        while (inputStream.available() > 0) {
            inputStream.read();
            Classex.insertSort(nums);
        }


        for (int x = 0; x < nums.length; x++)
            outputStream.write(nums[x]);
        inputStream.close();
    }


    private static void insertSort(char[] array) {
        for (int i = 1; i < array.length; i++) {
            char temp = array[i];
            for (int j = i - 1; j >= 0 && temp < array[j]; j--) {
                array[j + 1] = array[j];
                array[j] = temp;
            }
        }

    }
}